package com.gaeainfo.modules.alarm.feedback.service;

import com.gaeainfo.constant.GaeaConstant;
import com.gaeainfo.framework.core.utils.common.GaeaCommonUtil;
import com.gaeainfo.modules.alarm.feedback.dao.AlarmFeedbackDao;
import com.gaeainfo.modules.alarm.feedback.dto.FeedbackAttDto;
import com.gaeainfo.modules.alarm.feedback.dto.FeedbackDto;
import com.gaeainfo.modules.alarm.feedback.entity.FeedbackAttEntity;
import com.gaeainfo.modules.alarm.feedback.entity.FeedbackEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;

/**
 * Created by Caven on 2018/7/12.
 */
@Service("alarmFeedbackService")
public class AlarmFeedbackServiceImpl implements AlarmFeedbackService {

    @Autowired
    private AlarmFeedbackDao alarmFeedbackDao;

    @Override
    public String createFeedback(FeedbackDto feedbackDto) throws Exception {
        FeedbackEntity feedbackEntity = new FeedbackEntity();
        feedbackEntity.setFeedbackId(feedbackDto.getFeedbackId());
        feedbackEntity.setProcessId(feedbackDto.getProcessId());
        feedbackEntity.setDeptId(feedbackDto.getDeptId());
        feedbackEntity.setDeptName(feedbackDto.getDeptName());
        feedbackEntity.setSpaceTypeCode(feedbackDto.getSpaceTypeCode());
        feedbackEntity.setSpaceTypeName(feedbackDto.getSpaceTypeName());
        feedbackEntity.setResultTypeCode(feedbackDto.getResultTypeCode());
        feedbackEntity.setResultTypeName(feedbackDto.getResultTypeName());
        feedbackEntity.setPushed(pushFeedback(feedbackDto) ? "1" : "0");
        feedbackEntity.setFeedbackTime(new Timestamp(System.currentTimeMillis()));
        feedbackEntity.setReason(feedbackDto.getReason());
        alarmFeedbackDao.createFeedback(feedbackEntity);
        this.createFeedbackAtt(feedbackDto);
        return feedbackDto.getFeedbackId();
    }

    @Override
    public String updateFeedback(FeedbackDto feedbackDto) throws Exception {
        FeedbackEntity feedbackEntity = alarmFeedbackDao.findFeedbackById(feedbackDto.getFeedbackId());
        feedbackEntity.setSpaceTypeCode(feedbackDto.getSpaceTypeCode());
        feedbackEntity.setSpaceTypeName(feedbackDto.getSpaceTypeName());
        feedbackEntity.setResultTypeCode(feedbackDto.getResultTypeCode());
        feedbackEntity.setResultTypeName(feedbackDto.getResultTypeName());
        feedbackEntity.setPushed(pushFeedback(feedbackDto) ? "1" : "0");
        feedbackEntity.setReason(feedbackDto.getReason());
        alarmFeedbackDao.updateFeedback(feedbackEntity);
        this.createFeedbackAtt(feedbackDto);
        return feedbackDto.getFeedbackId();
    }

    @Override
    public FeedbackDto getFeedback(String processId) throws Exception {
        FeedbackDto feedbackDto = new FeedbackDto();
        FeedbackEntity feedbackEntity = alarmFeedbackDao.findFeedbackByProcessId(processId);
        feedbackDto.setFeedbackId(feedbackEntity.getFeedbackId());
        feedbackDto.setProcessId(feedbackEntity.getProcessId());
        feedbackDto.setDeptName(feedbackEntity.getDeptName());
        feedbackDto.setPoliceNo(feedbackEntity.getPoliceNo());
        feedbackDto.setPoliceName(feedbackEntity.getPoliceName());
        feedbackDto.setAlarmLevel(feedbackEntity.getAlarmLevel());
        feedbackDto.setSpaceTypeCode(feedbackEntity.getSpaceTypeCode());
        feedbackDto.setSpaceTypeName(feedbackEntity.getSpaceTypeName());
        feedbackDto.setResultTypeCode(feedbackEntity.getResultTypeCode());
        feedbackDto.setResultTypeName(feedbackEntity.getResultTypeName());
        feedbackDto.setReason(feedbackEntity.getReason());
        //feedbackDto.setSituation(feedbackEntity.gets);
        return null;
    }

    @Override
    public String deleteFeedbackAtt(String feedbackId, String fileId) throws Exception {
        FeedbackAttEntity feedbackAttEntity = alarmFeedbackDao.findFeedBackAttById(feedbackId, fileId);
        if (feedbackAttEntity == null) {
            return GaeaConstant.Result.FAIL;
        }
        alarmFeedbackDao.deleteFeedbackAtt(feedbackAttEntity);
        return GaeaConstant.Result.SUCCESS;
    }

    private void createFeedbackAtt(FeedbackDto feedbackDto) throws Exception {
        for (FeedbackAttDto feedbackAttDto : feedbackDto.getAttList()) {
            FeedbackAttEntity feedbackAttEntity = new FeedbackAttEntity();
            feedbackAttEntity.setAttId(GaeaCommonUtil.generateUUID(""));
            feedbackAttEntity.setFeedbackId(feedbackDto.getFeedbackId());
            feedbackAttEntity.setFileType(feedbackAttDto.getFileType());
            feedbackAttEntity.setFileName(feedbackAttDto.getFileName());
            feedbackAttEntity.setCreatedTime(new Timestamp(System.currentTimeMillis()));
            alarmFeedbackDao.createFeedbackAtt(feedbackAttEntity);
        }
    }

    private boolean pushFeedback(FeedbackDto feedbackDto) {
        try {

        } catch (Exception e) {

        }
        return false;
    }
}
